#include "../include/sql.h"
#include "../include/tcp.server.h"

int update_tab(char *name, char *id)
{
    char sql[1024] = {0};

    sprintf(sql, "update tab set id = '%s' where name = '%s';", id, name);

    if (sqlite_grop(sql) < 0)
    {
        return -1;
    }

    return 0;
}

int insert_tab(char *name)
{
    char sql[1024] = {0};

    sprintf(sql, "insert into student() values('%s');", name);
    if (sqlite_grop(sql) < 0)
    {
        return -1;
    }

    return 0;
}

// int find_tab(char *name)
// {
//     char sql[1024] = {0};

//     strcpy(sql, "select * from %s where name = ")
// }
int delete_tab(char *name)
{
    char sql[1024] = {0};

    sprintf(sql, "delete from tab where name = \'%s\';", name);
    if (sqlite_grop(sql) < 0)
    {
        return -1;
    }

    return 0;
}
int create_tab(char *name) // 创建用户表
{
    char sql[1024] = {0};
    sprintf(sql, "create table if not exists %s(user text, passwd text);", name);

    if (sqlite_grop(sql) < 0)
    {
        return -1;
    }

    return 0;
}
int init_sqlite3(char *name) // 创建以name为名字的数据表
{
    char sql[1024] = {0};

    sprintf(sql, "create table if not exists %s(id integer, name text, age integer, tel text, addr text, mailbox text,primary key(id));", name);

    if (sqlite_grop(sql) < 0)
    {
        return -1;
    }

    return 0;
}
int sqlite_grop(char *sql)
{
    int ret;
    sqlite3 *db;

    char *errmsg;
    if ((ret = sqlite3_open("./data.db", &db)) != SQLITE_OK)
    {
        printf("sql open error:%s\n", sqlite3_errmsg(db));
        sqlite3_close(db);
        return -1;
    }

    if ((ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg)) != SQLITE_OK)
    {
        printf("sql exec error:%s\n", sqlite3_errmsg(db));
        sqlite3_close(db);
        return -1;
    }

    sqlite3_close(db);
    return 0;
}

// int callback(void *data, int argc, char **argv, char **azColName)
// {    //查询数据库中是否存在想查的表

//     if (1 == argc)
//     {
//         int iTableExist = atoi(*(argv));
//         if (data != NULL)
//         {
//             int *ptr = (int *)data;
//             *ptr = iTableExist;
//             if (*ptr > 0)
//             {
//                 *((int *)data) = 1;
//             }
//         }
//     }
//     return 0;
// }
int find_change(void *data, int columnCount, char **columnValue, char **columnName)
{
    *((int *)data) = exist;
    return 0;
}
int show_result(void *pata, int columnCount, char **columnValue, char **columnName)
{
    online info;
    memset(&info, 0, sizeof(online));
    
    int fd = *((int*)pata);
    info.signal = FIND;
    for (int i = 0; i < columnCount; i++)
    {
        switch(i)
        {
            case 0:
            {
                info.id = atoi(columnValue[i]);
                break;
            }
            case 1:
            {
                strcpy(info.name,columnValue[i]);
                break;
            }
            case 2:
            {
                info.age = atoi(columnValue[i]);
                break;
            }
            case 3:
            {
                strcpy(info.tel,columnValue[i]);
                break;
            }
            case 4:
            {
                strcpy(info.addr,columnValue[i]);
                break;
            }
            case 5:
            {
                strcpy(info.mailbox,columnValue[i]);
                break;
            }
            default:
            {                
                break;
            }
        }
        
    }
    send(fd, &info, sizeof(online), 0);
    return 0;
}

int find_tab(char *sql, int num, int fd)
{
    int ret;
    sqlite3 *db;

    char *errmsg;
    if ((ret = sqlite3_open("./data.db", &db)) != SQLITE_OK)
    {
        printf("sql open error:%s\n", sqlite3_errmsg(db));
        sqlite3_close(db);
        return -1;
    }
    if (find_result== num)
    {
        if ((ret = sqlite3_exec(db, sql, show_result, (void *)&fd, &errmsg)) != SQLITE_OK)
        {
            printf("sql exec error:%s\n", sqlite3_errmsg(db));
            sqlite3_close(db);
            printf("info error\n");
            return -1;
        }
    }
    else if (find_exist == num)
    {
        if ((ret = sqlite3_exec(db, sql, find_change, (void *)&num, &errmsg)) != SQLITE_OK)
        {
            printf("sql exec error:%s\n", sqlite3_errmsg(db));
            sqlite3_close(db);
            return -1;
        }
    }
    // if(num == 1)
    // {
    //     printf("user is exist!\n");
    // }
    // else
    // {
    //     printf("user is not exist!\n");
    // }
    sqlite3_close(db);
        //printf("SEE signol3 = %d\n", info->signal);
    return num;
}